import {Component, OnInit} from '@angular/core';
import {NavController, ModalController, NavParams} from 'ionic-angular';
import {StoragePage} from "../storage/storage";
import {KcmodelPage} from "../ckmodel/kcmodel";
import {ResultPage} from "../result/result";
import {BarcodeScanner} from "@ionic-native/barcode-scanner";
import {interfaceUrls} from "../../../providers/serverUrls";
import {services} from "../../../providers/service";
import {OuterService} from "../provider/storage-service";
import {AppPopup} from "../../../util/popup";
import {StorageService} from "../../../providers/storage-service";

@Component({
    selector: 'management', templateUrl: 'management.html',
})
export class ManagementPage implements OnInit {
    imgURL: string = interfaceUrls.imgUrl;
    showAlert = true;
    thisProductInfo;            //从出库单传过来的订单信息
    jxs = {                     //经销商信息
        "companyName": '', "name": '', "mobile": '', "receivedCompanyId": ''
    };
    show_jxs = false;
    receivedCompanyId;
    scanData;

    constructor(public navCtrl: NavController,
                public navParam: NavParams,
                private service: services,
                private outerServ: OuterService,
                public modalCtrl: ModalController,
                public barcodeScanner: BarcodeScanner,
                private popup: AppPopup,
                private storageServ: StorageService,) {
        this.thisProductInfo = navParam.get("thisProductInfo");
        this.scanData = navParam.get("scanData");
        console.log('outId:', this.outerServ.outerId);
        this.outerServ.getOuterDetail({outId: this.outerServ.outerId})
            .then(res => {
                console.log("ManagementPage:出库管理获取出库单详情，", res);
                this.thisProductInfo = res.infoData;
            })
            .catch(err => {
                console.info("获取出库单详情", err);
            });
    }

    presentProfileModal() {
        let profileModal = this.modalCtrl.create(KcmodelPage, {"thisProductInfo": this.thisProductInfo});
        profileModal.onDidDismiss((data, receivedCompanyId) => {
            if (data) {
                console.log(data);
                this.jxs = data;
                this.receivedCompanyId = receivedCompanyId;
                this.show_jxs = true;
            }

        })
        profileModal.present();
    }

    toShowAlert() {
        this.showAlert = false;
    }

    btnCancel() {
        this.showAlert = true;
    }

    btnChoose() {
        this.showAlert = true;
        this.navCtrl.setRoot(StoragePage);
    }

    //确认出库
    chuku() {
        console.log(this.jxs)
        let param = {
            "id": this.thisProductInfo.id,                      //出库单id，必传
            "receivedCompanyId": this.receivedCompanyId         //收货经销商企业id，必传
        }
        this.storageServ.complete(param)
            .then(res => {
                console.info("出库单_完成出库成功：", res);
                this.navCtrl.setRoot(StoragePage);
            })
            .catch(err => {
                console.error("出库单_完成出库失败：", err);
                this.popup.alert(err.errorMassage);
            });
    }

    //继续 -- 出库扫码
    toScan() {
        this.barcodeScanner.scan().then((barcodeData) => {
            if (barcodeData.text == null || barcodeData.text == '') {
                console.log('取消了扫码');
                this.service.isReturn = true;
                return;
            }
            this.service.boxNo = barcodeData.text;
            let param = {
                "boxNo": barcodeData.text,        //箱码
                "outId": this.thisProductInfo.id,                   //出库单ID
                "outNo": this.thisProductInfo.outNo,                //出库单号
                "productId": this.thisProductInfo.productId,        //产品ID
            }
            this.storageServ.scan(param)
                .then(res => {
                    console.info("出库单_出库扫码成功：", res);
                    this.navCtrl.push(ResultPage, {data: res, thisProductInfo: this.thisProductInfo});
                })
                .catch(err => {
                    console.error("出库单_出库扫码失败：", err);
                    this.navCtrl.push(ResultPage, {data: err, thisProductInfo: this.thisProductInfo});
                });
        }, (err) => {
            // An error occurred
            console.error(err);
        });

    }

    ngOnInit() {
    }

}
